/****************************************************************************
 *  QLyrics - A simple lyrics viewer.                                       *
 *  Copyright (C) 2008  hsys <hanshuiys@gmail.com>                          *
 *                                                                          *
 *  This program is free software; you can redistribute it and/or modify    *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation; either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  This program is distributed in the hope that it will be useful,         *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with this program; if not, write to the Free Software Foundation, *
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA       *
 ****************************************************************************/

#include "lyricsverticalpanel.h"
#include "configmanager.h"

LyricsVerticalPanel::LyricsVerticalPanel(QWidget* parent) : LyricsPanel(parent) {
};

const QString& LyricsVerticalPanel::Builder::getPanelName() const {
    static QString name = LyricsVerticalPanel::tr("Vertical");
    return name;
};

LyricsPanel* LyricsVerticalPanel::Builder::create(QWidget* parent) const {
    return new LyricsVerticalPanel(parent);
};

void LyricsVerticalPanel::drawFrame(LyricsTime time) {
    // save old lyircs iterator to determin whether current lyrics changed
    static const LyricsData::LyricsElement* pold = NULL;

    if (!m_lyrics || m_lyrics->isEmpty())
        return;
    // find current lyrics
    LyricsData::const_iterator px = m_lyrics->find(time), py = px;
    // if nothing found, exit
    if (px == m_lyrics->end())
        return;

    ConfigManager* cfg = ConfigManager::getInstance();

    // determin where should i print the current line of lyrics
    int timeLength = (px + 1 == m_lyrics->end()) ? 0 : (px + 1)->offset - px->offset;
    int lineHeight = m_painter.fontMetrics().height();
    int delayFactor = (timeLength == 0) ? 0 : (time - px->offset) * 0x100 / timeLength;
    int printY = height() * cfg->value("Style/VerticalLyricsFactor", 0.5).toDouble() - lineHeight * delayFactor / 0x100;
    int printX;
    
    // current lyrics changed
    if (pold != &(*px)) {
        pold = &(*px);
        updateInterval(getTextHeight(), timeLength);
    };

    // find the first line of lyrics i need to print 
    while (printY > 0 && px != m_lyrics->begin()) {
        printY -= lineHeight;
        --px;
    };

    // ok, now i print the lyrics line by line
    while (printY < height() + getTextHeight() && px != m_lyrics->end()) {
        printX = (width() - m_painter.fontMetrics().width(px->lyrics)) / 2;
        if (px == py) {
            // print highlighted line
            drawText(m_painter, printX, printY,
                getFadingColor(time - px->offset, timeLength),
                px->lyrics);
        } else {
            drawText(m_painter, printX, printY,
                cfg->value("Style/FgColor", Qt::darkGreen).value<QColor>(),
                px->lyrics);
        };
        printY += getTextHeight();
        ++px;
    };
    //m_painter.drawLine(0, height() / 2, width(), height() / 2);
};

int LyricsVerticalPanel::getTextHeight() {
    ConfigManager* cfg = ConfigManager::getInstance();
    int spacing = cfg->value("Style/VerticalLyricsSpacing", 2).toInt();
    return fontMetrics().height() + spacing;
};

